Instances spun up by OpenStack are ephemeral. For storage that persists across an instance lifecycle, it is necessary to attach and mount a volume to the instance.
This guide assumes that access to the OpenStack CLI is installed and the user has authenticated with the cloud.
If you don’t already have a keypair, create one and redirect the private part of the key to the default .ssh key store directory.
$ openstack keypair create KEYPAIR_NAME > ~/.ssh/KEYPAIR_FILENAME
where:
KEYPAIR_NAME
is an non-unique identifier for the key pair,KEYPAIR_FILENAME
is the file in which the private part of the key pair is to be saved.Change the permission on the private part of the keypair.
sudo chmod 600 ~/.ssh/KEYPAIR_FILENAME
Create the volume.
$ openstack volume create --size VOLUME_SIZE --type VOLUME_TYPE VOLUME_NAME
where:
VOLUME_SIZE
is the desired volume size in Gb,VOLUME_TYPE
is selected from the output of $ openstack volume type list (default rbd),VOLUME_NAME
is a non-unique identifier for the volume.If one doesn’t already exist, spin up an instance.
$ openstack server create --image INSTANCE_IMAGE_NAME --flavor INSTANCE_FLAVOR --key-name KEYPAIR_NAME --network INSTANCE_NETWORK_NAME INSTANCE_NAME
where:
INSTANCE_IMAGE_NAME
is selected from the output of $ openstack image listINSTANCE_FLAVOR
is selected from the output of $ openstack flavor listINSTANCE_NAME
is a non-unique identifier for the instanceINSTANCE_NETWORK_NAME
is the network name unto which to attach the instance.Attach the volume to the instance.
$ openstack server add volume INSTANCE_NAME VOLUME_NAME
Assign a floating IP from an externally facing pool.
$ openstack server add floating ip INSTANCE_NAME EXTERNAL_FLOATING_IP
where:
EXTERNAL_FLOATING_IP
is an address from the pool.Remove any existing entry for the EXTERNAL_FLOATING_IP in the known_hosts file.
$ ssh-keygen -f "/home/$(USER)/.ssh/known_hosts" -R EXTERNAL_FLOATING_IP > /dev/null
Partition the disk volume (note that the device mount location may be different than /dev/vdb if another storage device has already been mounted; check output of $ sudo fdisk -l if unsure) and create a new filesystem on the partition.
$ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo parted -s /dev/vdb mklabel msdos && sudo parted -s /dev/vdb mkpart primary ext4 1MiB 100% && sudo mkfs -t ext4 /dev/vdb1'
Add an entry for mounting the partition to the file systems table, /etc/fstab.
$ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo su -c "echo /dev/vdb1 /mnt/volume1 ext4 rw 0 0 >> /etc/fstab"'
Create a directory at the mount point, mount the volume to the instance filesystem and symlink the volume’s mount directory to /data.
$ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo mkdir /mnt/volume1 && sudo mount /dev/vdb1 && sudo ln -s /mnt/volume1/ /data'
where:
IAM_USERNAME
is your username for non-federated login to IAM (see Prerequisites).The mount command can be flagged with -r to issue a read only mounted filesystem.
Change the mount directory permissions to allow read/write access for all users.
$ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo chmod o+w /mnt/volume1'